/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.SafetyPualificationBean;
import com.koron.purpose.service.api.SafetyPualificationService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "safetyPualification", description = "安全资质")
@RestController
@RequestMapping("/safetyPualification")
public class SafetyPualificationController {
    private final static  Logger logger = LoggerFactory.getLogger(SafetyPualificationController.class);

    @Autowired
    private SafetyPualificationService safetyPualificationService;

    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wcorpCode", value = "水司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pualificationType", value = "资质类型 code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acquisitionTimeStart", value = "获取时间(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acquisitionTimeStop", value = "获取时间(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryForList.htm", method = RequestMethod.GET)
    public MessageBean queryForList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String wcorpCode, @ApiIgnore String corpName, @ApiIgnore String pualificationType,
                                    @ApiIgnore String acquisitionTimeStart, @ApiIgnore String acquisitionTimeStop,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyPualificationService.queryForList(wcorpCode, corpName, pualificationType,
                acquisitionTimeStart, acquisitionTimeStop,
                page, pageSize);
    }

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/queryById.htm", method = RequestMethod.GET)
    public MessageBean queryById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyPualificationService.queryById(id);
    }

    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteSafetyPualiById.htm", method = RequestMethod.POST)
    @Log(title = "根据ID删除安全资质", businessModule = "安全文化", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean deleteSafetyPualiById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyPualificationService.deleteSafetyPualiById(id);
    }

    @ApiOperation("新增安全资质")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wcorpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wcorpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pualificationType", value = "资质类型 code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pualificationTypeName", value = "资质类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "credential", value = "证书名称（等级）code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "credentialName", value = "证书名称（等级）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acquisitionTime", value = "获取时间(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "effectiveTime", value = "获取时间(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/insertSafetyPuali.htm", method = RequestMethod.POST)
    @Log(title = "新增安全资质", businessModule = "安全文化", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean insertSafetyPuali(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                         @ApiIgnore SafetyPualificationBean bean,
                                         @RequestParam(value = "files", required = false) MultipartFile[] files) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyPualificationService.insertSafetyPuali(user, bean, files);
    }

    @ApiOperation("修改安全资质")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pualificationType", value = "资质类型 code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pualificationTypeName", value = "资质类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "credential", value = "证书名称（等级）code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "credentialName", value = "证书名称（等级）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acquisitionTime", value = "获取时间", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "accessorys", value = "附件(参数格式为JSON字符串，" +
                    "如( [{'filename':'xxx','fileUrl':'xxx'},{'filename':'xxx','fileUrl':'xxx'}] ))",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/updateSafetyPuali.htm", method = RequestMethod.POST)
    @Log(title = "修改安全资质", businessModule = "安全文化", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean updateSafetyPuali(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                         @ApiIgnore SafetyPualificationBean bean,
                                         @RequestParam(value = "files", required = false) MultipartFile[] files) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyPualificationService.updateSafetyPuali(user, bean, files);
    }

    @ApiOperation("导出安全资质")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wcorpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pualificationType", value = "资质类型 code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acquisitionTimeStart", value = "获取时间(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acquisitionTimeStop", value = "获取时间(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/exportSafetyPualification.htm", method = RequestMethod.GET)
    @Log(title = "导出安全资质", businessModule = "安全文化", businessManipulation = BusinessManipulation.EXPORT)
    public HttpEntity<?> exportSafetyPualification(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String wcorpCode, @ApiIgnore String corpName, @ApiIgnore String pualificationType,
                                                   @ApiIgnore String acquisitionTimeStart, @ApiIgnore String acquisitionTimeStop) {
        if (null == user) {
            return null;
        }
        List<Map> list = safetyPualificationService.queryForList(wcorpCode, corpName, pualificationType, acquisitionTimeStart, acquisitionTimeStop);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File file = new File("static/excel/ExportSafetyPualificationExport.xlsx");
        try (
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/ExportSafetyPualificationExport.xlsx");
        ) {
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("安全资质", file, "SafetyPualificationExport.btl", map);
    }
}
